using System.Collections.Generic;
using System.Data;
using Magiq.Behaviors;
using Magiq.Update;

namespace Magiq.Databases.Update
{
    public class DatabaseUpdateBehavior<T> : DefaultBehavior, IDatabaseUpdateBehavior<T> where T : class
    {
        private IOrmAdapter ormAdapter;
        private IUpdate<T> update;

        #region IDatabaseUpdateBehavior<T> Members

        public IEnumerable<DatabaseUpdate<T>> BuildDatabaseUpdates()
        {
            return ormAdapter.BuildDatabaseUpdates(update);
        }

        public void Initialize(IOrmAdapter ormAdapter, IUpdate<T> update)
        {
            this.update = update;
            this.ormAdapter = ormAdapter;
        }

        public IDbCommand BuildCommand(DatabaseUpdate<T> databaseUpdate)
        {
            var where = databaseUpdate.Source;
            var builder = new UpdateCommandBuilder(databaseUpdate.OrmAdapter, where,
                                                   databaseUpdate.ColumnAssignments);
            return builder.GetCommand();
        }

        #endregion
    }
}